//
//  ballView.m
//  JiCaiBao
//
//  Created by rgx on 16/11/11.
//  Copyright © 2016年 rgx. All rights reserved.
//

#import "ballView.h"


#define  ballViewRedColor  [UIColor colorWithRed:247.0/255.0 green:79.0/255.0 blue:35.0/255.0 alpha:1]

#define  ballViewBlueColor [UIColor colorWithRed:33.0/255.0 green:123.0/255.0 blue:213.0/255.0 alpha:1]

#define  ballViewClickColor  [UIColor whiteColor]

@interface ballView()
{
    ballViewType mtype;
    __unsafe_unretained id  ballDelegate;
}


@end

@implementation ballView

@synthesize  isClicked;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame ballType:(ballViewType)type delegate:(id)gate tag:(NSString *)tag need:(bool)value;
{
    self = [self initWithFrame:frame];
    if(self){
        mtype  = type;
        ballDelegate = gate;
        
        [self addTarget:self action:@selector(selfClick:) forControlEvents:UIControlEventTouchUpInside];
        [self setBackgroundImage:[UIImage imageNamed:@"BetGrayBall"] forState:UIControlStateNormal];

//        [self.layer setMasksToBounds:YES];
//        self.layer.cornerRadius = self.frame.size.width/2;
        
//        [self.layer setBorderWidth:1.0]; //边框宽度
//        
//        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//        CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
//        
//        if (mtype == ballViewType_blue) {
//            
//            colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 1, 1 });
//        }else{
//            
//            colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
//            
//        }
//        [self.layer setBorderColor:colorref];//边框颜色
//
        if(mtype ==ballViewType_Red){
            
            [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        else
            [self setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
        
        [self setTitle:[NSString stringWithFormat:@"%@",tag] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13];
    }
    
    return self;
}

-(void)selfClick:(id)sender
{
    isClicked = ! isClicked;
    
    [self flashSelf];
    if(ballDelegate && [ballDelegate respondsToSelector:@selector(ballViewClick:)])
        [ballDelegate ballViewClick:self];
}



-(void)flashSelf
{
    
    if(isClicked){
        [self setTitleColor:ballViewClickColor forState:UIControlStateNormal];
        if(mtype ==ballViewType_Red){
            UIImage *redImage = [UIImage imageNamed:@"BetRedBall"];

            [self setBackgroundImage:redImage forState:UIControlStateNormal];
        }
        else{
            UIImage *blueImage = [UIImage imageNamed:@"BetBlueBall"];
            [self setBackgroundImage:blueImage forState:UIControlStateNormal];
        }
    }
    else{
        UIImage *img =[UIImage imageNamed:@"BetGrayBall"];
    
        [self setBackgroundImage:img forState:UIControlStateNormal];
        if(mtype ==ballViewType_Red){
            [self setTitleColor:ballViewRedColor forState:UIControlStateNormal];
        }
        else
            [self setTitleColor:ballViewBlueColor forState:UIControlStateNormal];
    }
    
}
@end
